package vues;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;

import sourceCode.Rail;


public class VueRail extends Vue {

    protected static int largeurRail = 10;

    private boolean estEnSurbrillance = false;

    private Color couleurNormal = new Color(130, 250, 100);
    private Color couleurSurbrillance = new Color(200, 0, 0);
    private Color couleurSelectionne = new Color(0, 130, 250);

    private Rail rail;
    private VueAeroport vueAeroport;
    private int largeurRailMetre;
    private Polygon polygoneRail;

    public VueRail(VueAeroport v, Rail r) {
        super();
        this.rail = r;
        this.vueAeroport = v;
        this.largeurRailMetre = 1;
        Point depart = this.vueAeroport.convertCoordonnesToPixels(rail.getNoeudDepart().getCoordonnees());
        Point arrivee = this.vueAeroport.convertCoordonnesToPixels(rail.getNoeudArrivee().getCoordonnees());
        this.polygoneRail = DessinePolygone(depart, arrivee);
    }

    protected Polygon DessinePolygone(Point depart, Point arrivee) {
        //Rail horizontal ou presque
        int[] listeX = new int[4];
        int[] listeY = new int[4];
        if (Math.abs(depart.getY() - arrivee.getY()) < 30) {
            listeX[0] = depart.x;
            listeX[1] = arrivee.x;
            listeX[2] = arrivee.x;
            listeX[3] = depart.x;
            listeY[0] = depart.y - this.largeurRail / 2;
            listeY[1] = arrivee.y - this.largeurRail / 2;
            listeY[2] = arrivee.y + this.largeurRail / 2;
            listeY[3] = depart.y + this.largeurRail / 2;
        }
        //Rail vertical ou presque
        else if (Math.abs(depart.getX() - arrivee.getX()) < 30) {
            listeX[0] = depart.x + this.largeurRail / 2;
            listeX[1] = arrivee.x + this.largeurRail / 2;
            listeX[2] = arrivee.x - this.largeurRail / 2;
            listeX[3] = depart.x - this.largeurRail / 2;
            listeY[0] = depart.y;
            listeY[1] = arrivee.y;
            listeY[2] = arrivee.y;
            listeY[3] = depart.y;
        }
        //oblique ascendant
        else {

            listeX[0] = (int)(depart.x - (this.largeurRail / (2 * this.rail.getAlpha())));
            listeX[1] = (int)(arrivee.x - (this.largeurRail / (2 * this.rail.getAlpha())));
            listeX[2] = (int)(arrivee.x + (this.largeurRail / (2 * this.rail.getAlpha())));
            listeX[3] = (int)(depart.x + (this.largeurRail / (2 * this.rail.getAlpha())));
            listeY[0] = depart.y;
            listeY[1] = arrivee.y;
            listeY[2] = arrivee.y;
            listeY[3] = depart.y;
        }

        return new Polygon(listeX, listeY, listeX.length);
    }

    @Override
    public boolean estCliquee(int x, int y) {
        return this.polygoneRail.contains(x, y);
    }

    @Override
    public void dessiner(Graphics g) {
        Color cTemp = g.getColor();
        if (this.estEnSurbrillance) {
            g.setColor(this.couleurSurbrillance);
        } else if (estSelectionnee()) {
            g.setColor(this.couleurSelectionne);
        } else {
            g.setColor(this.couleurNormal);
        }
        g.drawLine(x1, y1, x2, y2);
        g.fillPolygon(this.polygoneRail); //dessin du rectangle qui repr�sente le chariot

        g.setColor(cTemp);
        return;
    }

    @Override
    public String getType() {
        return "VueRail";
    }

    public void setEstEnSurbrillance(boolean estEnSurbrillance) {
        this.estEnSurbrillance = estEnSurbrillance;
    }

    public Rail getRail() {
        return this.rail;
    }
}
